<template>
  <div>
    <a-form-model ref="ha_appsettingForm"
                  :rules="rules"
                  :model="ha_appsetting">
      <a-form-model-item :label-col="labelCol"
                         :wrapper-col="wrapperCol"
                         label="应用分组"
                         prop="appgroup"
                         has-feedback>
        <a-input v-if="!isView"
                 allow-clear
                 v-model="ha_appsetting.appgroup"
                 :maxLength="30"
                 placeholder="请输入应用分组" />
        <span v-text="ha_appsetting.appgroup"
              v-else></span>
      </a-form-model-item>
      <a-form-model-item :label-col="labelCol"
                         :wrapper-col="wrapperCol"
                         label="应用名称"
                         prop="appname"
                         has-feedback>
        <a-input v-if="!isView"
                 allow-clear
                 v-model="ha_appsetting.appname"
                 :maxLength="30"
                 placeholder="请输入应用名称" />
        <span v-text="ha_appsetting.appname"
              v-else></span>
      </a-form-model-item>
      <a-form-model-item :label-col="labelCol"
                         :wrapper-col="wrapperCol"
                         label="应用值"
                         prop="appvalue"
                         has-feedback>
        <a-input v-if="!isView"
                 allow-clear
                 v-model="ha_appsetting.appvalue"
                 :maxLength="100"
                 placeholder="请输入应用值" />
        <span v-text="ha_appsetting.appvalue"
              v-else></span>
      </a-form-model-item>
      <a-form-model-item :label-col="labelCol"
                         :wrapper-col="wrapperCol"
                         label="描述"
                         prop="describe"
                         has-feedback>
        <a-input v-if="!isView"
                 allow-clear
                 v-model="ha_appsetting.describe"
                 :maxLength="500"
                 placeholder="请输入描述" />
        <span v-text="ha_appsetting.describe"
              v-else></span>
      </a-form-model-item>
      <a-form-model-item :label-col="labelCol"
                         :wrapper-col="wrapperCol"
                         label="数据类型"
                         prop="datatype"
                         has-feedback>
        <a-select v-if="!isView"
                  v-model="ha_appsetting.datatype"
                  style="width: 110px"
                  placeholder="请输入数据类型">
          <a-select-option value="string">
            string
          </a-select-option>
          <a-select-option value="int">
            int
          </a-select-option>
          <a-select-option value="double">
            double
          </a-select-option>
          <a-select-option value="char">
            char
          </a-select-option>
          <a-select-option value="boolean">
            boolean
          </a-select-option>
        </a-select>
        <span v-text="ha_appsetting.datatype"
              v-else></span>
      </a-form-model-item>
      <a-form-model-item>
        <div style="text-align: center;">
          <a-input type="hidden"
                   :disabled="true"
                   v-model="ha_appsetting.id" />
          <a-button :style="{ marginRight: '8px' }"
                    icon="close"
                    @click="onClose">
            关闭
          </a-button>
          <a-button :style="{ marginRight: '8px'}"
                    icon="reload"
                    @click="resetForm"
                    v-if="!isView">
            重置
          </a-button>
          <a-button type="primary"
                    @click="onSubmit"
                    icon="save"
                    :loading="loading"
                    v-if="!isView">
            保存
          </a-button>
        </div>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
/* eslint-disable */
import { saveHa_appsetting, viewHa_appsetting } from '@/api/base/ha_appsetting'
// 没用到枚举请删除
import { queryEnumerationValuesByName } from '@/api/base/enumeration'
export default {
  data () {
    return {
      labelCol: { span: 6 }, // label宽
      wrapperCol: { span: 18 }, // 组件宽
      loading: false, //按钮显示
      isView: false, //是否查看
      ha_appsetting: {
        id: '',
        appgroup: '',
        appname: '',
        appvalue: '',
        describe: '',
        datatype: 'string',
      }, //系统配置信息
      rules: {
      },
    }
  },
  created () {

  },
  methods: {
    /**
      *  查询枚举值,没有用到枚举请删除
     */
    findEnumByName () {
      const parameter = [
      ]
      parameter.forEach(item => {
        queryEnumerationValuesByName({ 'enName': item.enumName }).then(res => {
          console.log('query' + item.enumName, res.data)
          this[`${item.enname}List`] = res.data
          //this[`${item.enname}List`].unshift({ id: '', describe: '全部' })
        })
      })
    },
    /**
     * 调用父组件的方法收起抽屉
     */
    onClose () {
      this.$emit('close')
    },
    /**
     * 提供给父组件调用
     * 点击新增进入时重置表单数据
     */
    resetHa_appsetting () {
      this.isView = false
      this.resetForm()
    },
    /**
     * 提供给父组件调用
     * 查询用户信息
     */
    findHa_appsettingById (id, isView) {
      this.isView = isView
      this.resetForm()

      viewHa_appsetting(id).then(res => {
        this.ha_appsetting = res.data
        /**
       *  枚举如果不能显示中文打开下面的代码
          */
      })
    },
    /**
     * 重置表单
     */
    resetForm () {
      this.$refs.ha_appsettingForm.resetFields();
    },
    /**
     * 提交表单
     */
    onSubmit () {
      this.$refs.ha_appsettingForm.validate(valid => {
        this.loading = true
        if (valid) {
          delete this.ha_appsetting.primaryKey
          console.log('submit!', this.ha_appsetting)
          const savem = this.$message.loading('数据正在保存中...', 0)
          saveHa_appsetting(this.ha_appsetting).then(res => {
            if (res.code !== 0) {
              setTimeout(savem, 0)
              this.$message.error('系统配置保存失败')
              this.loading = false
            } else {
              setTimeout(savem, 0)
              this.$message.success('保存成功')
              this.$emit('reloadList')
              this.$emit('close')
              this.loading = false
            }
          })
        } else {
          this.loading = false
          // 加延时器的目的是为了vue实例更新未完成，我们可能获取DOM失败的问题
          // 利用原生js的方式，获取所有失败的类名，获得一个数组，取第一个元素获取焦点
          setTimeout(() => {
            const isError = document.getElementsByClassName("has-error");
            const input = isError[0].querySelector('input');
            //console.log(input)
            if (input !== null && input !== 'undefind' && input !== '') {
              isError[0].querySelector('input').focus() ? isError[0].querySelector('input').focus() : isError[0].querySelector('input').click()
            } else {
              isError[0].querySelector('textarea').focus()
            }
          }, 10)
          return false;
        }
      });
    },
  }
}
</script>
<style lang="less" src="@/style/base.less">
</style>